package com.ddwl.order.service;


import com.ddwl.common.feign.api.GoodsApi;
import com.ddwl.order.dao.OrderInfoDao;
import com.ddwl.order.dao.OrderSpuDao;
import com.ddwl.order.dao.UpdateDao;
import com.ddwl.order.dao.model.OrderAddr;
import com.ddwl.order.dao.model.OrderInfo;
import com.ddwl.order.dao.model.OrderSpu;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.CategorySearchBo;
import com.ddwl.schema.vo.goods.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据订正service
 *
 * @author zach
 * @Date :2020-06-02 14:23.
 */
@Service
@RefreshScope
@Slf4j
public class UpdateService {

    @Resource
    private UpdateDao updateDao;
    
    @Resource
    private OrderInfoDao orderInfoDao;

    @Resource
    private OrderSpuDao orderSpuDao;

    @Resource
    private GoodsApi goodsApi;


    @Resource
    private OrderAddressServer orderAddresServer;
    /**
     * 更新订单信息
     * @return
     */
    public void updateAddrOrder() {

        List<OrderAddr> list = updateDao.updateAddr();




        List<String> ids=new ArrayList<>();
        for (OrderAddr addr: list) {
            if (addr.getSkuId()==null||addr.getSkuId()==""){
                ids.add(addr.getOrderId());
                OrderAddr add=new OrderAddr();
                add.setId(addr.getId());
                add.setDflag(1);
                updateDao.updateByPrimaryKeySelective(add);
            }
/*            if (addr.getProvinceName()==null||addr.getProvinceName()==""){
                OrderAddr add=new OrderAddr();
                add.setId(addr.getId());
                RegionVo regionVoRestVo = gateApi.geoRegion(addr.getRegionId()).getResult();
                add.setProvinceName(regionVoRestVo.getProvinceName());
                add.setCityName(regionVoRestVo.getCityName());
                add.setDistrictName(regionVoRestVo.getDistrictName());
                updateDao.updateByPrimaryKeySelective(add);
            }*/
        }

        List<OrderInfo> orderInfos = orderInfoDao.updateOrderList(ids);


        for (OrderInfo orderInfo:orderInfos) {
            for (OrderAddr orderAddr:list) {
                if (orderAddr.getOrderId().equals(orderInfo.getId())){
                    orderAddresServer.updateOrderAddr(orderAddr, orderInfo);
                }
            }
        }
    }


    /**
     * 更新订单信息
     * @return
     */
    public void updateSupplier() {

        PageBo<CategorySearchBo> pageBo=new PageBo<>();
        pageBo.setPageSize(40);
        List<CategoryVo> list = goodsApi.page(pageBo).getResult().getData();

        List<OrderSpu> spuList = orderSpuDao.selectAll();

        for (OrderSpu spu: spuList) {
            for (CategoryVo vo:list) {
                if (vo.getId().equals(spu.getCategoryId())){
                    OrderInfo orderInfo=new OrderInfo();
                    orderInfo.setId(spu.getOrderId());
                    orderInfo.setMasterOrderId(spu.getOrderId());
                    orderInfo.setSupplierId(vo.getSupplierId());
                    orderInfoDao.updateByPrimaryKeySelective(orderInfo);
                }
            }
        }

    }






}
